package SwingController;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;

import JDBC.CmsDatabaseConnection;
import SwingModel.CmsCourse;
import SwingModel.CmsInstructor;
import SwingModel.CmsStudent;
import SwingModel.CmsUser;
import SwingView.CmsAccountPanel;
import SwingView.CmsLoginView;
import SwingView.CmsMainView;
import SwingView.CmsPageSelector.CmsPage;

public class ALLogin implements ActionListener{
	
	//---------------------------------------------------------
	//METHOD: actionPerformed	
	//---------------------------------------------------------	
	@Override
	public void actionPerformed(ActionEvent e){
		
		CmsLoginView loginView = CmsPresenter.loginView;
		
		String username = loginView.getJtfUserName().getText();
		String password = new String(loginView.getJtfPassword().getPassword());
		
		//disable login button
		JButton loginButton =(JButton) e.getSource();
		loginButton.setEnabled(false);
		
		try{
			if (authenticate(username,password)){
				
				CmsPresenter.startMainView(fetchUserData(username));

			}
			else{
				loginView.notifyAuthenticationFail();
				loginButton.setEnabled(true); //enable the loginButton again
			}
		} catch(Exception ex){}
	}

	
	//---------------------------------------------------------
	//METHOD: fetchUserData	
	//---------------------------------------------------------	
	/**
	 * this method fetch user first name, last name, and instantiate
	 * an instance of the user either as an instructor or a student
	 * based on the role he/she is assigned in the DB
	 * 
	 * @param username
	 * @return
	 */
	public static CmsUser fetchUserData(String username){
		String firstName = "";
		String lastName = "";
		int isInstructor = 0;
		CmsUser user = null;
		
		CmsDatabaseConnection dCon = null;
		ResultSet resultSet = null;
		PreparedStatement pstmt = null;
		
		try{
			dCon = new CmsDatabaseConnection();
			pstmt = dCon.getPreparedStatement();
			pstmt = dCon.connection.prepareStatement("SELECT * FROM User WHERE username = ?");
			
			pstmt.setString(1,username);
			
			resultSet = pstmt.executeQuery();

		
			//parse user info:
			if(resultSet.next()){ 
				isInstructor = resultSet.getInt("IsInstructor");
				firstName = resultSet.getString("firstname");
				lastName = resultSet.getString("lastname");
			}
			pstmt.close();
			dCon.close();
			if(isInstructor==1){
				user = new CmsInstructor(username, firstName, lastName, true); // true == fetch list of course from DB
			}
			else{
				user = new CmsStudent(username, firstName, lastName,true); //true == fetch list of course from DB
			}
		}
		catch(Exception e){ }
		
		return user;
	}
	
	//---------------------------------------------------------
	//METHOD: authenticate
	//---------------------------------------------------------
	/**
	 * This method validate and authenticate the user
	 * @param userName
	 * @param password
	 * @return
	 */
	public static boolean authenticate(String userName, String password){
		CmsDatabaseConnection dCon = null;
		ResultSet resultSet = null;
		PreparedStatement pstmt = null;

		boolean result;
		
		try{
			dCon = new CmsDatabaseConnection();
			pstmt = dCon.getPreparedStatement();
			pstmt = dCon.connection.prepareStatement("SELECT * FROM User WHERE username = ?");
			
			pstmt.setString(1,userName);
			
			resultSet = pstmt.executeQuery();
		
			//Validate username and password:
			if(resultSet.next()){ 
				if(password.compareTo(resultSet.getString("password")) == 0){
					result = true;
				}
				else {//user found, but wrong credential
					result = false;
				}
			}
			else { //No username found
				result = false; 
			}
			pstmt.close();
			dCon.close();
		}
		catch(Exception e){ result = false;}
		
		return result;
	}
}
